package com.fanyouzhao.ehome.service.impl;

import com.fanyouzhao.ehome.dao.UserDao;
import com.fanyouzhao.ehome.dao.impl.UserDaoImpl;
import com.fanyouzhao.ehome.entity.Cp;
import com.fanyouzhao.ehome.entity.User;
import com.fanyouzhao.ehome.service.UserService;

import java.util.List;

/**
 * @author fanyouzhao
 * @date 2021年3月31日 下午6:16:47
 */
public class UserServiceImpl implements UserService {

    // UserDao对象解决具体的持久化操作
    UserDao userDao = new UserDaoImpl();

    @Override
    public User register(User user) throws Exception {
        // 判断用户名是否存在
        User selectUser = userDao.selectByPhone(user.getPhone());
        if (selectUser == null) {
            // 不存在就继续注册
            Integer lines = userDao.insert(user);
            if (lines > 0) {
                selectUser = userDao.selectByPhone(user.getPhone());
                return selectUser;
            }
        } else {
            return null;
        }
        return null;
    }

    @Override
    public Boolean isReg(String phone) throws Exception {
        User selectUser = userDao.selectByPhone(phone);
        return selectUser != null;
    }



    @Override
    public User login(String phone) throws Exception {
        return userDao.selectByPhone(phone);
    }

    @Override
    public List<User> selectAll() throws Exception {
        List<User> allUserList = userDao.selectAll();
        return allUserList;
    }

    @Override
    public List<User> selectByPage(Integer page, Integer size) throws Exception {
        return userDao.selectByPage(page, size);
    }

    @Override
    public User selectByPhone(String phone) throws Exception {
        return userDao.selectByPhone(phone);
    }

    @Override
    public List<Cp> selectCpsNotUsedByPhone(String phone) throws Exception{
        return userDao.selectCpsNotUsedByPhone(phone);
    }
    @Override
    public List<Cp> selectCpsUsedByPhone(String phone) throws Exception{
        return userDao.selectCpsUsedByPhone(phone);
    }

    @Override
    public List<User> searchByPhoneorName(String searchString, Integer page, Integer size) throws Exception {
        return userDao.selectLikeNameorPhoneByPage(searchString,page,size);
    }

    @Override
    public List<Cp> selectNotGetCpsByPhone(String phone) throws Exception {
        return userDao.selectNotGetCpsByPhone(phone);
    }

    @Override
    public List<Cp> selectGetCpsByPhone(String phone) throws Exception {
        return userDao.selectGetCpsByPhone(phone);
    }

    @Override
    public List<Cp> selectOverExpireCpsByPhone(String phone) throws Exception {
        return userDao.selectOverExpireCpsByPhone(phone);
    }

    @Override
    public Boolean getCpn(Integer cpId, String phone) throws Exception {
        return userDao.getCpn(cpId, phone) > 0;
    }

    @Override
    public Integer getCount() throws Exception {
        return userDao.getCount();
    }

    @Override
    public Integer getCountSearch(String searchString) throws Exception {
        return userDao.getCountSearch(searchString);
    }


    @Override
    public Boolean remove(String username, String password) throws Exception {
        return userDao.deleteByUsername(username) > 0;
    }

    @Override
    public Boolean deleteById(Integer id) throws Exception {
        return userDao.deleteById(id) > 0;
    }

    @Override
    public Boolean update(Integer id, User user) throws Exception {
        return null;
    }

    @Override
    public Boolean updateIsLogin(User user) throws Exception {
        return null;
    }

    @Override
    public Boolean updatePassword(User user) throws Exception {
        return null;
    }

}
